package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    /**
     * 添加购物车
     * @param cartParam
     * @return
     */
    @Override
    public Cart add(Cart cartParam) {
        //根据参数中的套餐id或菜品id,查询购物车表
        LambdaQueryWrapper<Cart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        wrapper.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        //查询购物车
        Cart dbcart = cartMapper.selectOne(wrapper);

        //判断购物车是否为空
        if (dbcart!=null){
            dbcart.setNumber(dbcart.getNumber()+1);
            //更新购物车
            cartMapper.updateById(dbcart);
            return dbcart;
        }else {
            //购物车不存在
            cartParam.setUserId(UserHolder.get().getId());
            cartParam.setNumber(1);
            cartParam.setCreateTime(new Date());
            //添加购物车
            cartMapper.insert(cartParam);
            return cartParam;
        }
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<Cart> findAll() {
        LambdaQueryWrapper<Cart>  wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        List<Cart> cartList = cartMapper.selectList(wrapper);
        return cartList;
    }

    /**
     * 修改购物车
     * @param cartParam
     * @return
     */
    @Override
    public Cart subUpdate(Cart cartParam) {
        //设置查询购物车条件
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());//只能修改自己购物车,根据自己的用户id查询
        wrapper.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        wrapper.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        //查询
        Cart dbcart = cartMapper.selectOne(wrapper);
        //判断购物车的数量否为1
        if (dbcart.getNumber()==1){
            cartMapper.deleteById(dbcart.getId());
            return null;
        }else {
            //购物车数量不唯一
            dbcart.setNumber(dbcart.getNumber()-1);
            cartMapper.updateById(dbcart);
            return dbcart;
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> wrapper =new LambdaQueryWrapper<>();
        //根君用户的id删除购物车信息
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }
}
